//
// Created by WenDaojiang on 15/11/22.
// Copyright (c) 2015 文道江. All rights reserved.
//

#import "HybridAlipayAction.h"


@implementation HybridAlipayAction

/**
 *  支付接口
 *
 *  @param orderStr       订单信息
 *  @param schemeStr      调用支付的app注册在info.plist中的scheme
 */
- (void) payOrder:(id)params {
    id order = params[@"order"];
    NSString *schemeStr = params[@"schemeStr"];
    if(schemeStr == nil){
        [self callback:NO data:@"请填写调用支付的app注册在info.plist中的scheme"];
        return;
    }

    if([schemeStr isEqualToString:@""]){
        NSArray *urlTypes = [[NSBundle mainBundle] infoDictionary][@"CFBundleURLTypes"];
        if (urlTypes != nil && urlTypes.count > 0){
            schemeStr = urlTypes[0][@"CFBundleURLSchemes"][0];
        }
    }

    if (order){
        NSString *orderStr = nil;
        if ([order isKindOfClass:[NSDictionary class]]){
            orderStr = [NSString stringWithFormat:@"partner=\"%@\"&"
                                                          "seller_id=\"%@\"&"
                                                          "out_trade_no=\"%@\"&"
                                                          "subject=\"%@\"&"
                                                          "body=\"%@\"&"
                                                          "total_fee=\"%@\"&"
                                                          "notify_url=\"%@\"&"
                                                          "service=\"%@\"&"
                                                          "payment_type=\"%@\"&"
                                                          "_input_charset=\"%@\"&"
                                                          "it_b_pay=\"%@\"&"
                                                          "return_url=\"%@\"&"
                                                          "sign=\"%@\"&"
                                                          "sign_type=\"%@\"",
                                                  order[@"partner"],
                                                  order[@"sellerId"],
                                                  order[@"outTradeNo"],
                                                  order[@"subject"],
                                                  order[@"body"],
                                                  order[@"totalFee"],
                                                  order[@"notifyUrl"],
                                                  order[@"service"],
                                                  order[@"paymentType"],
                                                  order[@"inputCharset"],
                                                  order[@"itBPay"],
                                                  order[@"showUrl"],
                                                  [self URLEncode:order[@"sign"] UsingEncoding:NSUTF8StringEncoding],
                                                  order[@"signType"]
            ];
        }else{
            orderStr = order;
        }

        NSLog(@"order info = %@", orderStr);

        [[AlipaySDK defaultService] payOrder:orderStr fromScheme:schemeStr callback:^(NSDictionary *resultDic) {
            NSLog(@"alipay result %d %@", [resultDic[@"resultStatus"] intValue] ,resultDic);
            //callback([resultDic[@"resultStatus"] intValue] == 9000, resultDic);
            [self callback:[resultDic[@"resultStatus"] intValue] == 9000 data:resultDic];
        }];
    }else{
        //callback(NO, @"请传正确的 order 参数.");
        [self callback:NO data:@"请传正确的 order 参数."];
    }
}


#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (NSString *)URLEncode:(NSString *)string UsingEncoding:(NSStringEncoding)encoding {
    return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
            (__bridge CFStringRef)string,
            NULL,
            (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
            CFStringConvertNSStringEncodingToEncoding(encoding));
}
#pragma clang diagnostic pop

@end